<?php

namespace App\Controller\Admin;

use App\Model\ClassHour;
use App\Model\ConsultantApply;
use App\Model\ConsultantDatum;
use App\Model\ConsultantGood;
use App\Model\User;
use App\Model\UserDatum;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * @Controller(prefix="/admin/user")
 */
class UserController extends AdminBaseController
{

    /**
     * 处理咨询师审核
     * @RequestMapping(path="handleAuthentication")
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function handleAuthentication()
    {

        $params = $this->request->post();

        $this->validate($params, [
            'id' => 'required',
            'status' => 'required',
        ]);

        $info = ConsultantApply::where(['id' => $params['id'], 'status' => 0])->first();
        if (is_null($info)) {
            return $this->response->adminFail('未找到审核记录');
        }
        $info = $info->toArray();

        //如果失败就直接拒绝
        if ($params['status'] !=1){
            ConsultantApply::where(['id' => $params['id'], 'status' => 0])->update(['status'=>2,'update_time'=>time()]);
            return  $this->response->adminSuccess();
        }





        $userInfo = UserDatum::where(['uid' => $info['uid']])->first();
        if (is_null($userInfo)) {
            return $this->response->adminFail('用户信息出错');
        }


        $data = [
            'uid'=>$info['uid'],
            'name' => $info['name'],
            'type' => $info['user_type'],
            'account_front' => $info['account_front'],
            'account_back' => $info['account_back'],
            'introduce' => $info['introduce'],
            'language' => $info['language'],
            'education' => $info['education'],
            'education_img' => $info['education_img'],
            'certificate' => $info['certificate'],
            'certificate_img' => $info['certificate_img'],
            'direction' => $info['direction'],
            'crowd' => $info['crowd'],
            'genre' => $info['genre'],
            'style' => $info['style'],
            'status' => $params['status'],
            'cover' => $userInfo['head'],
            'years' => $info['years'],
            'country' => $info['country'],
            'start_work' => $info['start_work'],
            'price' => $info['price'],
            'class_id' => $info['class_id'],
        ];

        $consultantDatum = ConsultantDatum::where(['uid' => $info['uid']])->first();

        Db::beginTransaction();
        try {

            //处理价格
            $priceUpdate = ConsultantGood::updatePrice($info['uid'], $info['class_id'], $info['price'], $info['country']);

            if (!$priceUpdate) {
                return $this->response->adminFail('设置价格出错');
            }

            //处理心理咨询师资料
            if (is_null($consultantDatum)) {//第一次申请入住
                $data['create_time'] = $info['create_time'];
                $data['check_time'] = time();

                ConsultantDatum::insert($data);

            } else {
                $data['update_time'] = time();
                ConsultantDatum::where(['uid' => $info['uid']])->update($data);
            }

            //处理用户表状态
            User::where(['id' => $info['uid']])->update(['role' => $info['user_type'], 'update_time' => time()]);

            //处理审核表状态

            ConsultantApply::where(['id' => $params['id'], 'status' => 0])->update(['status'=>1,'update_time'=>time()]);



            Db::commit();

            return $this->response->adminSuccess();

        } catch (\Exception $e) {

            var_dump($e->getMessage());
            Db::rollBack();
            logger()->info('审批出错', [$e->getMessage()]);
            return $this->response->adminFail();
        }


    }

}